const app = getApp();
const db = wx.cloud.database();
const _ = db.command
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    treasures: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    headerHeight: app.globalData.navHeight + 20,
    frozen: 0,
    showtab: 0,
    myTreasures: [],
    gold: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {

    changeTab(e) {
      this.setData({
        showtab: e.currentTarget.dataset.index
      })
      if (e.currentTarget.dataset.index == 1) {
        this.loadMyTreasure();
      }
    },
    /* 获取玩家现有宝物数据*/

    loadMyTreasure() {
      wx.cloud.callFunction({
        name: 'treasure',
        data: {
          op: 'getMyTreasure'
        }
      }).then(res => {
        console.log('myTreasure', res.result)
        this.setData({
              myTreasures: res.result
            })
      })
    },
   
    /**
     * 买入
     */
    buyOne(e) {

      this.setData({
        frozen: e.currentTarget.dataset.type
      })
      let treasure = {
        op: 'buy',
        type: e.currentTarget.dataset.type,
        price: e.currentTarget.dataset.price,
        src: e.currentTarget.dataset.src,

      };
      wx.cloud.callFunction({
        name: 'treasure',
        data: treasure
      }).then(res => {
        if (res.result == 'goldNotEnough') {
          wx.showToast({
            icon: 'none',
            title: '你的金币不够了',
          })
        } else {         
          this.triggerEvent("getUserInfo");
        }
        this.setData({
          frozen: 0
        })
      })


    },
    /**
     * 卖出
     */
    saleOne(e) {

      this.setData({
        frozen: e.currentTarget.dataset.type
      })
      let treasure = {
        op: 'sale',
        type: e.currentTarget.dataset.type,
        price: e.currentTarget.dataset.price,
     
      };
      wx.cloud.callFunction({
        name: 'treasure',
        data: treasure
      }).then(res => {
     
        this.triggerEvent("getUserInfo");
        this.setData({
          frozen: 0
        })
        this.loadMyTreasure();
      })
    }
  },

})